﻿using System.Collections;
using System.Collections.Generic;
using Const;
using UnityEngine;

public class InputController : MonoBehaviour
{
    // Start is called before the first frame update
    private InputContext _inputContext;
    private Vector2 _clickPos;
    private float _time;
    private float _offsetX;
    private float _offsetY;
    
    
    void Start()
    {
        _inputContext = Contexts.sharedInstance.input;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), 
                                        Vector2.zero, 
                                        100);
            if (hit.collider != null)
            {
                
                _clickPos = hit.transform.position;
                _inputContext.ReplaceComponentsClick((int)_clickPos.x,(int)_clickPos.y);
                //Debug.Log("X:"+_clickPos.x+"Y:"+_clickPos.y);
            }

            _time = 0;
            _offsetX = 0;
            _offsetY = 0;
        }

        if (Input.GetMouseButton(0))
        {
            //TODO：滑动组件未能工作
           // Debug.Log(_time);
            if (_time < 0.5f)
            {
                _time += Time.deltaTime;
                _offsetX += Input.GetAxis("Mouse X");
                _offsetY += Input.GetAxis("Mouse Y");
            }
            else
            {
               // Debug.Log("slide");
                Slide();
                
            }

            if (Input.GetMouseButtonDown(0) && _time < 0.5f && _time > 0.1f)
            {
                Slide();
            }
            
        }
    }

    private void Slide()
    {
        //（条件表达式）？选项1：选项2
        slideDirection direction = Mathf.Abs(_offsetX) > Mathf.Abs(_offsetY)
            ? (_offsetX > 0 ? slideDirection.RIGHT : slideDirection.LEFT)
            : (_offsetY > 0 ? slideDirection.UP: slideDirection.DOWN);
        Debug.Log(direction);
        _inputContext.ReplaceComponentsSlide(
            new customData((int)_clickPos.x
                          ,(int)_clickPos.y)
                          ,direction);
        
        
    }
}
